/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.chl.da.controllerBeans;

import edu.chl.da.ejb.core.Airport;
import edu.chl.da.ejb.core.Destination;
import edu.chl.da.ejb.ctrl.IManageAirport;
import edu.chl.da.ejb.ctrl.IManageDestination;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

/**
 * Controller bean managing listing, getting and removal events of destinations
 * @author Kristofer
 */
@ManagedBean(name = "ManageDestinationController")
@RequestScoped
public class ManageDestinationController {

    private String selectedDestinationName;
    private boolean success = false;
    private String errorMsg;

    @EJB
    IManageDestination imd;

    @EJB
    IManageAirport ima;

    /** Creates a new instance of ManageDestinationBean */
    public ManageDestinationController() {

    }

    public String removeDestination(){
        Destination d = imd.getDestinationByName(selectedDestinationName);
        List<Airport> tempList = ima.getAirportsByDestination(d);

        for(Airport a : tempList){
            ima.removeAirport(a);
        }
        
        if(imd.removeDestination(d)) {
            return "/admin/manageDestination.jsf";
        }
        else{
         FacesContext.getCurrentInstance().addMessage(null,
                new FacesMessage("Gick ej att radera destinationen, kontrollera eventuella flygplatser"));
        return null;
        }
    }


    public String getSelectedDestinationName() {
        return selectedDestinationName;
    }

    public void setSelectedDestinationName(String selectedDestinationName) {
        this.selectedDestinationName = selectedDestinationName;
    }
}
